import java.sql.*;

public class DBUtil {
    // 数据库连接参数（请根据实际情况修改）
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/db_test";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";


    // 获取数据库连接
    public static Connection getConnection() {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //增删查改
    //查询数据库
    public void select(Connection conn, String sql) {
        //Statement 实例化对象：对数据库进行交互
        try {
            Statement statement = conn.createStatement();
            //创建结果集
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                System.out.print(resultSet.getInt(1)+"");
                System.out.print(resultSet.getString(2)+"");
                System.out.print(resultSet.getInt(3)+"");
                System.out.print(resultSet.getString(4)+"");
                System.out.print(resultSet.getString(5)+"");
                System.out.print(resultSet.getString(6)+"");
                System.out.println();
            }
            statement.close();
            resultSet.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
//    增加信息功能
    public void insert(Student student,Connection conn) {
        String sql = "insert into tb_student(name,age,gender,class,school) values(?,?,?,?,?)";
//    与数据库交互的类
        try {
            PreparedStatement ps =conn.prepareStatement(sql);
            ps.setString(1,student.getName());
            ps.setInt(2,student.getAge());
            ps.setString(3,student.getGender());
            ps.setString(4,student.getClass1());
            ps.setString(5,student.getSchool());
            int num = ps.executeUpdate();
            if(num > 0){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //    修改信息功能
    public void update(Student student,Connection conn) {
        String sql = "update tb_student set name=?,age=?,gender=?,class=?,school=? where id=?";
//    与数据库交互的类
        try {
            PreparedStatement ps =conn.prepareStatement(sql);
            ps.setString(1,student.getName());
            ps.setInt(2,student.getAge());
            ps.setString(3,student.getGender());
            ps.setString(4,student.getClass1());
            ps.setString(5,student.getSchool());
            ps.setInt(6,student.getId());
            int num = ps.executeUpdate();
            if(num > 0){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

        //    删除信息功能
        public void delete(int id,Connection conn) {
            String sql = "delete from tb_student where id=?";
//    与数据库交互的类
            try {
                PreparedStatement ps =conn.prepareStatement(sql);
                ps.setInt(1,id);
                int num = ps.executeUpdate();
                if(num > 0){
                    System.out.println("删除成功");
                }else {
                    System.out.println("删除失败");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
